package com.cn.psys.tools.excel;

import cn.hutool.core.date.DateUtil;
import jxl.Workbook;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.write.*;
import org.apache.commons.collections.map.LinkedMap;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.util.List;

public class ExcelUtil {
    public static void export(HttpServletResponse response, List<?> importlist, String[] attributeNames, String[] attribute, String objPath) {
        WritableWorkbook book = null;
        OutputStream outStream = null;
        List<Object> list_act = null;
        try {
            // 数据填写完毕，进行导出---------------------------------------------------------------------------------------------
            outStream = response.getOutputStream();// 取得输出流
            response.reset();// 清空输出流
            String timeTitle = DateUtil.now();
            response.setHeader("Content-disposition", "attachment; filename="
                    + new String("学生分班信息表(".getBytes("GB2312"), "8859_1")
                    + timeTitle + ").xls");// 设定输出文件头
            response.setContentType("octets/stream");// 定义输出类型

            // 打开一个文件的副本，并且指定数据写回到原文件
            book = Workbook.createWorkbook(outStream);
            // 添加一个工作表
            WritableSheet sheet = book.createSheet("学生分班信息表", 0);
            // 设置单元格的文字格式 表头
            WritableFont wf1 = new WritableFont(WritableFont.ARIAL, 22,
                    WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
                    Colour.BLACK);
            // 单元格的格式对象
            WritableCellFormat cellFormat1 = new WritableCellFormat(wf1);
            cellFormat1.setAlignment(jxl.format.Alignment.CENTRE);
            cellFormat1
                    .setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);

            WritableCellFormat cellFormat2 = new WritableCellFormat(wf1);
            cellFormat2.setAlignment(jxl.format.Alignment.CENTRE);
            cellFormat2
                    .setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
            // 设置边框及边框颜色
            // ==================================================表头
            // ===============================================列明
            WritableFont wf2 = new WritableFont(WritableFont.ARIAL, 9,
                    WritableFont.BOLD, false, UnderlineStyle.NO_UNDERLINE,
                    Colour.BLACK);

            // 单元格的格式对象
            WritableCellFormat cellFormat3 = new WritableCellFormat(wf2);
            // 设置边框及边框颜色
            cellFormat3.setBorder(Border.ALL, BorderLineStyle.THIN,
                    jxl.format.Colour.BLACK);
            cellFormat3.setAlignment(jxl.format.Alignment.CENTRE);
            cellFormat3
                    .setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
            cellFormat3.setWrap(true);
            // 列明======================================================
            // ====================内容=======================================
            // 导出内容
            WritableFont wf3 = new WritableFont(WritableFont.ARIAL, 9,
                    WritableFont.NO_BOLD, false, UnderlineStyle.NO_UNDERLINE,
                    Colour.BLACK);

            // 单元格的格式对象
            WritableCellFormat cellFormat4 = new WritableCellFormat(wf3);
            // 设置边框及边框颜色
            cellFormat4.setBorder(Border.ALL, BorderLineStyle.THIN,
                    jxl.format.Colour.BLACK);
            cellFormat4.setAlignment(jxl.format.Alignment.CENTRE);
            cellFormat4
                    .setVerticalAlignment(jxl.format.VerticalAlignment.CENTRE);
            cellFormat4.setWrap(true);
            // ====================内容=======================================
            sheet.mergeCells(0, 0, attributeNames.length-1, 0);// 第一行合并单元格
            sheet.addCell(new Label(0, 0, "学生分班信息表", cellFormat1));// 第一行表头
            for (int i = 0; i < attributeNames.length; i++) {
                sheet.setColumnView(0, 10); // 设置列的宽度
                sheet.addCell(new Label(i, 1, attributeNames[i], cellFormat3));// 列名
            }
            // 填充数据库的数据

            int dataRowIndex = 2;
            Class<?> clas = Class.forName(objPath);
            Field[] fields = clas.getDeclaredFields();
            LinkedMap map = null;
            for (int i = 0; i < importlist.size(); i++) {
                map = new LinkedMap();
                for (Field field : fields) {
                    field.setAccessible(true);
                    // System.out.println( field.getName()+ " " + field.get(importlist.get(i))); // 取得属性名
                    map.put(field.getName(), field.get(importlist.get(i)));
                }
                System.out.println(map);
                for (int j = 0; j < attribute.length; j++) {
                    sheet.addCell(new Label(j, i + dataRowIndex, String.valueOf(map.get(attribute[j])==null?"":map.get(attribute[j])), cellFormat4));
                }
            }
            // 将文件写出来
            book.write();
        } catch (Exception ex) {
            ex.printStackTrace();
        } finally {
            try {
                book.close();
                outStream.flush();
                outStream.close();
            } catch (WriteException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
}
